import { round } from './number';

/**
 * 数字格式化
 * @param {number|string} number - 要格式化的数字
 * @param decimals - 保留几位小数
 * @param decimalPoint - 小数点符号
 * @param thousandsSeparator - 千分位符号
 * @returns 格式化后的数字
 */
export function priceFormat(number: number | string, decimals: number | undefined = undefined, decimalPoint = '.', thousandsSeparator = ',') {
  // 提取数字(包含符号)
  const numbStr = String(number).replace(/[^0-9+-Ee.]/g, '');

  // 判断是否为有限数值
  const numb = Number(numbStr);
  /** 保留几位小数 */
  const prec = typeof decimals === 'number' ? Math.abs(decimals) : undefined;
  /** 分隔符 */
  const sep = String(thousandsSeparator || '');
  /** 小数点 */
  const dec = String(decimalPoint || '') || '.';

  /** 分割小数和整数 */
  let numbArr = String(prec !== undefined ? round(numb, prec) : numb).split('.');
  numbArr = numbArr.length > 0 ? numbArr : ['0'];
  const re = /(-?\d+)(\d{3})/;
  while (re.test(numbArr[0]!)) {
    numbArr[0] = numbArr[0]!.replace(re, `$1${sep}$2`);
  }
  let deci = String(numbArr[1] || '');
  if (prec !== undefined && deci.length < prec) {
    deci += new Array(prec - deci.length + 1).join('0');
  }
  return numbArr.join(dec);
}

/**
 * 大数字格式化为带单位的文本
 * @param value - 大数字
 * @param returnArray - 是否返回数组
 */
export function bigNumberTransform<T extends boolean>(value: number, returnArray?: T): T extends true ? [number, string] : string {
  const newValue: [number, string] = [0, ''];
  let fr = 1000;
  let num = 3;
  let text1 = '';
  let text2 = '';
  let fm = 1;
  if (isNaN(value)) {
    return (returnArray ? newValue : '') as T extends true ? [number, string] : string;
  }
  if (value < 0) {
    // eslint-disable-next-line no-param-reassign
    value = Math.abs(value);
    text2 = '-';
  }
  while (value / fr >= 1) {
    fr *= 10;
    num += 1;
  }
  if (num <= 4) {
    // 千
    newValue[0] = value;
    newValue[1] = '';
  } else if (num <= 8) {
    text1 = '万';
    fm = text1 === '万' ? 10000 : 10000000;
    if (value % fm === 0) {
      newValue[0] = Math.floor(value / fm);
    } else {
      newValue[0] = Number((value / fm).toFixed(2));
    }
    newValue[1] = text1;
  } else {
    // text1 = (num - 8) / 3 > 1 ? '千亿' : '亿'
    text1 = '亿';
    text1 = (num - 8) / 4 > 1 ? '万亿' : text1;
    text1 = (num - 8) / 7 > 1 ? '千万亿' : text1;
    text1 = (num - 8) / 10 > 1 ? '亿亿' : text1;
    fm = 1;
    if (text1 === '亿') {
      fm = 100000000;
    } else if (text1 === '千亿') {
      fm = 100000000000;
    } else if (text1 === '万亿') {
      fm = 1000000000000;
    } else if (text1 === '千万亿') {
      fm = 1000000000000000;
    } else {
      fm = 1000000000000000000;
    }
    if (value % fm === 0) {
      newValue[0] = Math.floor(value / fm);
    } else {
      newValue[0] = Number((value / fm).toFixed(2));
    }
    newValue[1] = text1;
  }
  if (value < 1000) {
    newValue[0] = value;
    newValue[1] = '元';
  }
  newValue[0] = text2 ? Number(text2 + newValue[0]) : newValue[0];
  return (returnArray ? newValue : `${newValue[0]}${newValue[1]}`) as T extends true ? [number, string] : string;
}
